package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.server.SectionOwnerUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.server.SectionOwnerUploadResponse;
import io.renren.common.utils.GUID;
import io.renren.modules.basedata.dao.OperlogDao;
import io.renren.modules.basedata.dao.SectionOwnerUploadDao;
import io.renren.modules.basedata.entity.tbl_Operlog;
import io.renren.modules.basedata.entity.tbl_sectionOwner;
import io.renren.modules.basedata.service.SectionOwnerUploadService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * @author djz
 * @version 上传收费公路业主信息
 */
@Service
public class SectionOwnerUploadServiceImpl implements SectionOwnerUploadService {

	private static final Logger log = Logger.getLogger(SectionOwnerUploadServiceImpl.class);

	@Autowired
	SectionOwnerUploadDao sectionOwnerUploadDao;
	@Autowired
	OperlogDao operlogDao;
	@Override
	public String sectionOwnerUpload() throws Exception {
		log.info("开始获取收费公路业主信息！");
		int operResult = 0;
		List<tbl_sectionOwner> list = null;
		tbl_sectionOwner sectionOwner = null;
		tbl_Operlog operlog = new tbl_Operlog();
		list = getSectionOwnerList();//获取收费公路业主信息
		if(list!=null&&list.size()>0){
			
			for(int i=0;i<list.size();i++){
				sectionOwner = list.get(i);
				//收费公路业主信息上传及变更接口
				SectionOwnerUploadRequest request = new SectionOwnerUploadRequest();
				request.setId(sectionOwner.getId());
				request.setName(sectionOwner.getName());
				request.setTel(sectionOwner.getTel());
				request.setContact(sectionOwner.getContact());
				request.setAddress(sectionOwner.getAddress());
//				request.setBank(sectionOwner.getBank());
//				request.setBankAddr(sectionOwner.getBankAddr());
//				request.setBankAccount(sectionOwner.getBankAccount());
//				request.setTaxpayerCode(sectionOwner.getTaxpayerCode());
//				request.setCreditCode(sectionOwner.getCreditCode());
				request.setOperation(sectionOwner.getFlag());
				//获取发送文件名
				String fileName = ETCCommHelper.getFileName(request,"65");
				//生成就json文件
//				CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);
					
				try{
					//指定响应的协议模型,调用upload发送数据
					SectionOwnerUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, SectionOwnerUploadResponse.class);
					log.info("上传收费公路业主信息成功,服务器返回:"+response.toJson());
					if(response!=null&&response.getInfo().equals("成功！")){

						//更新上传标志

						if(sectionOwnerUploadDao.updateSectionOwner(sectionOwner.getId())==1){
							log.info("更新收费公路业主信息表上传标志成功!");
							operResult = 1;
							operlog = getOperlog(23,1,sectionOwner.getId(),fileName ,operResult, response.getInfo(),"上传成功,更新成功");
						}else{
							log.info("更新收费公路业主信息表上传标志成功!");
							operlog = getOperlog(23,1,sectionOwner.getId(),fileName ,operResult, response.getInfo(),"上传成功,更新失败");
							operlogDao.insertlog(operlog);
							//sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
						}					
//						sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
					}else{
						log.info("上传收费公路业主信息失败!失败原因:"+response.getInfo());

						sectionOwnerUploadDao.updateSectionOwnerError(sectionOwner.getId());
						//sqlMapClient.update("tollBaseInfo_ORACLE.updateSectionOwnerError",sectionOwner.getId());
						
						//getOperlog（操作类型,传输类型,发送文件名,接收文件名,操作结果,部中心返回结果,说明）
						operlog = getOperlog(23,1 ,sectionOwner.getId(),fileName ,0,response.getInfo(), response.toJson());
						operlogDao.insertlog(operlog);
						//sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
					}
				}catch(IOException e){
					log.error("上传收费公路业主信息失败！错误信息:"+e.getMessage());
					
//					sqlMapClient.update("tollBaseInfo_ORACLE.updateSectionOwnerError",sectionOwner.getId());
					
					operlog = getOperlog(23, 1, sectionOwner.getId(), fileName, 0,"IO异常", "出现IO异常,数据上传失败:"+e.getMessage());
					operlogDao.insertlog(operlog);
					//sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
					e.printStackTrace();
					continue;
				}catch (ApiRequestException apie) {
					
					log.error("上传收费公路业主信息失败！错误信息:"+apie.getMessage());
					
//					sqlMapClient.update("tollBaseInfo_ORACLE.updateSectionOwnerError",sectionOwner.getId());
					
					operlog = getOperlog(23, 1, sectionOwner.getId(), fileName, 0,String.valueOf(apie.getErrCode()), "出现Apie异常,数据上传失败:"+apie.getMessage());
					operlogDao.insertlog(operlog);
					//sqlMapClient.insert("operlog_ORACLE.insertlog",operlog);
					apie.printStackTrace();
					continue;
				}
			}
				return "上传收费公路业主信息结束！";	
		}else{
			return "无收费公路业主信息,上传结束...";
		}
	}
	
	//获取收费公路业主信息
	@SuppressWarnings("unchecked")
	public List<tbl_sectionOwner> getSectionOwnerList() throws IOException{
		List<tbl_sectionOwner> list;
		try{
			list = sectionOwnerUploadDao.getSectionOwnerList();
		}catch(Exception e){
			e.printStackTrace();
			log.error("获取收费公路业主信息失败！错误信息:",e);
			throw new IOException("获取收费公路业主信息失败:"+e.getMessage());
		}
		return list;
	}

	//获取operlog信息
	public tbl_Operlog getOperlog(int operType,int softType,String sendFileName,String reFileName,int operResult,String reResult, String content){
		tbl_Operlog operlog = new tbl_Operlog();
		operlog.setWastesn(new GUID().toString());
		operlog.setOperTime(new Date());
		operlog.setOpertype(operType);
		operlog.setSoftType(softType);
		operlog.setSendFileName(sendFileName);
		operlog.setReFileName(reFileName);
		operlog.setOperResult(operResult);
		operlog.setReResult(reResult);
		operlog.setContent(content);
		return operlog;
	}
	
}
